﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using IHISCETeim.BLL;
using IHISCETeim.Library;
using IHISCETeim.Model;

namespace IHISCETeim.Web.MachineSys.UserModel
{
    public partial class UReturnVisitEdit : PageBase
    {
        protected override Permission[] PowerKey
        {
            get
            {
                return new Permission[] { Permission.修改 };
            }
        }
        protected override string DWZRef
        {
            get { return "UReturnVisitList"; }
        }

        /// <summary>
        /// 需要修改的ID
        /// </summary>
        protected int UWCID
        {
            get
            {
                return GetRequestValue<int>("UWCID");
            }
        }

        private UWarrantyCardInfo _obj;
        protected UWarrantyCardInfo obj
        {
            get
            {
                if (_obj == null)
                    _obj = UWarrantyCardController.GetByUWCID(UWCID);
                return _obj;
                //OMenuController.ge
            }
        }

        private UReturnVisitInfo _obj2;
        protected UReturnVisitInfo obj2
        {
            get
            {
                if (_obj2 == null)
                    _obj2 = UReturnVisitController.GetByUWCID(UWCID);
                return _obj2;
                //OMenuController.ge
            }
        }
        private U4008RecordInfo _obj3;
        protected U4008RecordInfo obj3
        {
            get
            {
                if (_obj3 == null)
                    _obj3 = U4008RecordController.GetByU4RID(obj2.U4RID);
                return _obj3;
                //OMenuController.ge
            }
        }
        private OUserInfo _obj4;
        protected OUserInfo obj4
        {
            get
            {
                if (_obj4 == null)
                    _obj4 = OUserController.GetByUserName(obj3.UserName);
                return _obj4;
                //OMenuController.ge
            }
        }

        protected string GetUWCIDDetail
        {
            get
            {
                List<UReturnVisitUsabilityDetailInfo> CurrDetail = UReturnVisitController.UReturnVisitUsabilityDetail_GetByUWCID(UWCID);

                string CurrDetailStr = "";
                foreach (UReturnVisitUsabilityDetailInfo itm in CurrDetail)
                {
                    CurrDetailStr += "," + itm.TempID.ToString() + ",";
                }

                return CurrDetailStr;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (GetRequestValue<string>("ajaxmethod", "") == "edit")
            {
                addmenu();
                
            }
            else if (GetRequestValue<string>("ajaxmethod", "") == "cancelu4")
            {
                int U4RID=GetRequestValue<int>("U4RID",0);
                Response.Write(U4008RecordController.Delete(U4RID) ? "" : "取消失败");
                Response.End();
            }
        }
        private void addmenu()
        {
            UReturnVisitInfo obj24 = Request.SerializableObject<UReturnVisitInfo>();

            U4008RecordInfo obj25 = new U4008RecordInfo();
            obj25.UserName = GetRequestValue<string>("dwz.orgLookup.UserName", "");
            if (!string.IsNullOrEmpty(obj25.UserName))
            {
                if ((!string.IsNullOrEmpty(obj24.otherRemark) || obj24.Guide<3))//如果没做50小时养,或有其它问题
                {
                    string Problemdescription = "新交机一个月回访问题:";
                    //if (!obj24.KeepGood50)
                    //    Problemdescription += "50小时无保养.";
                    if (obj24.Guide<3)
                        Problemdescription += "对经销商指导不满意.";
                    if (!string.IsNullOrEmpty(obj24.otherRemark))
                        Problemdescription += obj24.otherRemark;

                    U4008RecordController.Delete(obj2.U4RID);//做删除

                    obj25.URType = "一个月回访";
                    obj25.SerialNumber = obj.SerialNumber;
                    //obj25.UserName = GetRequestValue<string>("dwz.orgLookup.UserName", "");
                    obj25.CallDate = DateTime.Now;
                    obj25.WorkAddress = "";
                    obj25.WorkHours = obj24.WorkTime.ToString();
                    obj25.Problemdescription = Problemdescription;
                    // obj25.Problemdescription = "新交机一个月后50小时未做保养.  其它问题:" + obj24.otherRemark;
                    obj25.CustomerTel = obj.CustomerMobile;
                    obj25.AgentName = obj.AgentName;
                    obj25.U4RID = U4008RecordController.Add(obj25);
                }
            }
            obj24.U4RID = obj25.U4RID;

            //2012-10-26新增的不满意项
            string[] Usability_DetailS = GetRequestValue<string>("Usability_Detail",",").Split(new char[] { ',' });
            List<UReturnVisitUsabilityDetailInfo> list = new List<UReturnVisitUsabilityDetailInfo>();
            foreach (string itm in Usability_DetailS)
            {
                if (itm.Trim() != "")
                {
                    UReturnVisitUsabilityDetailInfo itmDetail = new UReturnVisitUsabilityDetailInfo();
                    itmDetail.TempID = Convert.ToInt32(itm);
                    itmDetail.UWCID = obj24.UWCID;

                    list.Add(itmDetail);
                }
            }

            AddLog(Permission.修改, obj, obj24);

            if (UReturnVisitController.Update(obj24, list))
                SuccessJson("修改成功", this.DWZRef);
            else
                ErrorJson("修改失败", this.DWZRef);
        }
    }
}